<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="公司logo" v-bind="validateInfos.logo">
	          <j-image-upload  v-model:value="formData.logo" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司简称" v-bind="validateInfos.name">
            <a-input v-model:value="formData.name" placeholder="请输入公司简称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司全称" v-bind="validateInfos.fullName">
            <a-input v-model:value="formData.fullName" placeholder="请输入公司全称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="所属行业" v-bind="validateInfos.industryId">
	          <a-input-number v-model:value="formData.industryId" placeholder="请输入所属行业" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="所在行业" v-bind="validateInfos.industryName">
            <a-input v-model:value="formData.industryName" placeholder="请输入所在行业" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="人员规模" v-bind="validateInfos.staffSize">
	          <j-dict-select-tag v-model:value="formData.staffSize" dictCode="staff_size" placeholder="请选择人员规模" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="企业性质" v-bind="validateInfos.companyNature">
	          <j-dict-select-tag v-model:value="formData.companyNature" dictCode="company_nature" placeholder="请选择企业性质" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司简介" v-bind="validateInfos.companyProfile">
	          <a-textarea v-model:value="formData.companyProfile" rows="4" placeholder="请输入公司简介" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司福利" v-bind="validateInfos.welfare">
            <a-input v-model:value="formData.welfare" placeholder="请输入公司福利" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="地址名称" v-bind="validateInfos.addressName">
            <a-input v-model:value="formData.addressName" placeholder="请输入地址名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司地址" v-bind="validateInfos.address">
            <a-input v-model:value="formData.address" placeholder="请输入公司地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="门牌号" v-bind="validateInfos.addressHouse">
            <a-input v-model:value="formData.addressHouse" placeholder="请输入门牌号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="地址经度" v-bind="validateInfos.longitude">
            <a-input v-model:value="formData.longitude" placeholder="请输入地址经度" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="地址纬度" v-bind="validateInfos.latitude">
            <a-input v-model:value="formData.latitude" placeholder="请输入地址纬度" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="工作开始时间" v-bind="validateInfos.startTime">
		        <time-picker placeholder="请选择工作开始时间" value-format="HH:mm:ss"  v-model:value="formData.startTime" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="工作结束时间" v-bind="validateInfos.endTime">
		        <time-picker placeholder="请选择工作结束时间" value-format="HH:mm:ss"  v-model:value="formData.endTime" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="休息时间" v-bind="validateInfos.restTime">
            <a-input v-model:value="formData.restTime" placeholder="请输入休息时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="加班情况" v-bind="validateInfos.workOvertime">
	          <j-dict-select-tag v-model:value="formData.workOvertime" dictCode="work_overtime" placeholder="请选择加班情况" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司相册，逗号分隔" v-bind="validateInfos.album">
            <a-input v-model:value="formData.album" placeholder="请输入公司相册，逗号分隔" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="营业执照" v-bind="validateInfos.businessLicense">
	          <j-image-upload  v-model:value="formData.businessLicense" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="企业名称" v-bind="validateInfos.realName">
            <a-input v-model:value="formData.realName" placeholder="请输入企业名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="法人名称" v-bind="validateInfos.legalPerson">
            <a-input v-model:value="formData.legalPerson" placeholder="请输入法人名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="营业执照号" v-bind="validateInfos.identity">
            <a-input v-model:value="formData.identity" placeholder="请输入营业执照号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="认证状态：0-默认，1-待审核，2-审核失败，3-审核通过" v-bind="validateInfos.verifyStatus">
	          <a-input-number v-model:value="formData.verifyStatus" placeholder="请输入认证状态：0-默认，1-待审核，2-审核失败，3-审核通过" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="失败原因" v-bind="validateInfos.refuseReason">
            <a-input v-model:value="formData.refuseReason" placeholder="请输入失败原因" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="提交认证时间" v-bind="validateInfos.addAuthTime">
		        <a-date-picker placeholder="请选择提交认证时间" v-model:value="formData.addAuthTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="审核时间" v-bind="validateInfos.authDate">
		        <a-date-picker placeholder="请选择审核时间" v-model:value="formData.authDate" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="浏览数量" v-bind="validateInfos.browseNumber">
	          <a-input-number v-model:value="formData.browseNumber" placeholder="请输入浏览数量" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="县级城市名称" v-bind="validateInfos.city">
            <a-input v-model:value="formData.city" placeholder="请输入县级城市名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="县级城市编码" v-bind="validateInfos.citycode">
            <a-input v-model:value="formData.citycode" placeholder="请输入县级城市编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="市级城市名称" v-bind="validateInfos.pcity">
            <a-input v-model:value="formData.pcity" placeholder="请输入市级城市名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="市级城市编码" v-bind="validateInfos.pcitycode">
            <a-input v-model:value="formData.pcitycode" placeholder="请输入市级城市编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="排序" v-bind="validateInfos.sort">
	          <a-input-number v-model:value="formData.sort" placeholder="请输入排序" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" v-bind="validateInfos.remark">
            <a-input v-model:value="formData.remark" placeholder="请输入备注" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="tenantId" v-bind="validateInfos.tenantId">
	          <a-input-number v-model:value="formData.tenantId" placeholder="请输入tenantId" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="删除状态(0-正常,1-已删除)" v-bind="validateInfos.delFlag">
	          <a-input-number v-model:value="formData.delFlag" placeholder="请输入删除状态(0-正常,1-已删除)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import moment from 'moment';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { TimePicker } from 'ant-design-vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../RmsCompany.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    disabled: { type: Boolean, default: false },
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    logo: '',   
    name: '',   
    fullName: '',   
    industryId: undefined,
    industryName: '',   
    staffSize: '',   
    companyNature: '',   
    companyProfile: '',   
    welfare: '',   
    addressName: '',   
    address: '',   
    addressHouse: '',   
    longitude: '',   
    latitude: '',   
    startTime: '',   
    endTime: '',   
    restTime: '',   
    workOvertime: '',   
    album: '',   
    businessLicense: '',   
    realName: '',   
    legalPerson: '',   
    identity: '',   
    verifyStatus: undefined,
    refuseReason: '',   
    addAuthTime: '',   
    authDate: '',   
    browseNumber: undefined,
    city: '',   
    citycode: '',   
    pcity: '',   
    pcitycode: '',   
    sort: undefined,
    remark: '',   
    tenantId: undefined,
    delFlag: undefined,
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });
  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
